package com.intelligent.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.intelligent.pojo.Warehouse;
import com.intelligent.service.WarehouseService;
import com.intelligent.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 仓库控制器
 */
@Api(tags = "仓库管理")
@RestController
@RequestMapping("/warehouse")
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;

    @ApiOperation("获取仓库列表")
    @GetMapping("/list")
    public Result<List<Warehouse>> list(@RequestParam(value = "pageSize", defaultValue = "100") Integer pageSize) {
        try {
            LambdaQueryWrapper<Warehouse> wrapper = new LambdaQueryWrapper<>();
            wrapper.last("LIMIT " + pageSize);
            List<Warehouse> warehouses = warehouseService.list(wrapper);
            return Result.success(warehouses);
        } catch (Exception e) {
            return Result.error("获取仓库列表失败: " + e.getMessage());
        }
    }

    @ApiOperation("根据ID获取仓库")
    @GetMapping("/{id}")
    public Result<Warehouse> getById(@PathVariable Long id) {
        try {
            Warehouse warehouse = warehouseService.getById(id);
            if (warehouse == null) {
                return Result.error("仓库不存在");
            }
            return Result.success(warehouse);
        } catch (Exception e) {
            return Result.error("获取仓库失败: " + e.getMessage());
        }
    }
}
